package com.learning.android.currencyconvertor;

import android.database.Cursor;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;


public class CurrencySelectItem {
	
	public Spinner fromCurrSpinner;
	public Spinner toCurrSpinner;
	
	//public ArrayAdapter<CharSequence> currListAdapter;
	public CurrencySpinnerArrayAdaper currListAdapter;
	
//	public TextView fromCurrSpinner;
//	public TextView toCurrSpinner;
	public EditText fromCurrRate;
	public EditText toCurrRate;
	
	public TextView versus;
	
	public Button currencySwapBtn;
	
	//TextChanged Watcher for from/to currency exchange rates
	public TextWatcher fromCurrTextWatcher;
	public TextWatcher toCurrTextWatcher;
	
//	private CurrencyConvertor parentListActivity;

	public void setUpAdapterForSpinner(CurrencyConvertor  parentListActivity, CurrencyDBAdapter currencyDBAdapter)
	{
		//currListAdapter = ArrayAdapter.createFromResource(parentListActivity, R.array.currencies, android.R.layout.simple_spinner_item);
		Cursor cursor = currencyDBAdapter.fetchCurrencyConfig(0);
		currListAdapter = new CurrencySpinnerArrayAdaper(cursor,parentListActivity);
		
		toCurrSpinner.setAdapter(currListAdapter);
	    fromCurrSpinner.setAdapter(currListAdapter);
	 }
	
	
	public static class CurrencySpinnerArrayAdaper extends BaseAdapter 
	{
		private Cursor currCursor;
		private LayoutInflater currSpinnerInflator;
		
		public CurrencySpinnerArrayAdaper(Cursor currenciesCursor,CurrencyConvertor parentListActivity)
		{
			currCursor = currenciesCursor;
			currSpinnerInflator = parentListActivity.getLayoutInflater();
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return currCursor.getCount();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return currCursor.moveToPosition(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(convertView == null)
			{
				convertView = currSpinnerInflator.inflate(R.layout.spinner_item_currency_selected, null);
			}
			
			currCursor.moveToPosition(position);
			((TextView)convertView.findViewById(R.id.currencyName)).setText(currCursor.getString(currCursor.getColumnIndex(CurrencyDBAdapter.KEY_CURRENCY)));
			((ImageView)convertView.findViewById(R.id.countryFlag)).setImageResource(currCursor.getInt(currCursor.getColumnIndex(CurrencyDBAdapter.KEY_FLAG)));
			
			return convertView;
		}
	}
}
